/**
 * 
 */
package com.nail.zhaogr.front.action;

import java.io.IOException;
import java.sql.Timestamp;

import javacommon.util.Md5Bean;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.util.WebUtils;

import com.nail.zhaogr.model.Members;
import com.nail.zhaogr.service.MembersManager;

/**
 * @author Administrator
 * 
 */
public class RegisterAction extends BaseAction{

	private MembersManager membersManager;
	
	protected static final String Register_index = "!/Index/index.do";

	private Members members;
	java.lang.Long id = null;
	private String[] items;
	private String imageCode;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			members = new Members();
		} else {
			members = (Members) membersManager.getById(id);
		}
	}

	public String indexMembers() {
		this.setTitle("会员注册_"+"找工人网"+"_");
		this.setMetaDesc("注册找工人网会员，享受便捷的找工人服务");
		return "/front/Members/create.jsp";
	}

	public String register() {
		if (imageCode != null
				&& imageCode.equalsIgnoreCase(ServletActionContext.getRequest()
						.getSession().getAttribute("imageCode")+"")) {
			members.setCreateDate(new Timestamp(System.currentTimeMillis()));
			members.setLastLogin(new Timestamp(System.currentTimeMillis()));
			members.setPassword(Md5Bean.getInstance().getMD5ofStr(
					members.getPassword()));
			members.setPoints(5);
			membersManager.save(members);
			WebUtils.setSessionAttribute(ServletActionContext.getRequest(), "memberInfo", members);
			return Register_index;
		} else {
			this.addActionMessage("验证码错误");
			return "/front/Members/create.jsp";
		}

	}

	public String checkUserName() {
		try {
			if (membersManager.checkLoginName(members.getLoginName())) {

				ServletActionContext.getResponse().getWriter().write("用户名存在");

			} else {
				ServletActionContext.getResponse().getWriter().write("");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String checkEmail() {
		try {
			if (membersManager.checkLoginName(members.getEmail())) {

				ServletActionContext.getResponse().getWriter().write("该邮箱地址已被使用");

			} else {
				ServletActionContext.getResponse().getWriter().write("");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String checkImageCode(){
		try {
			if (imageCode != null
					&& !imageCode.equalsIgnoreCase(ServletActionContext.getRequest()
							.getSession().getAttribute("imageCode")+"")) {
				ServletActionContext.getResponse().getWriter().write("验证码错误");

			} else {
				ServletActionContext.getResponse().getWriter().write("");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String getPwd(){
		return "";
	}
	

	public Object getModel() {
		return members;
	}

	public void setMembersManager(MembersManager membersManager) {
		this.membersManager = membersManager;
	}

	public String getImageCode() {
		return imageCode;
	}

	public void setImageCode(String imageCode) {
		this.imageCode = imageCode;
	}

}
